package com.icefish.control;

import com.alibaba.fastjson.JSONObject;
import com.icefish.bean.Shopping_cart;
import com.icefish.bean.fastjsonParse.Meta;
import com.icefish.dao.Shopping_cartDao;
import com.icefish.daoFactory.DaoFactory;
import com.icefish.utils.Init_Req_Resp_Config;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deleteShoppingCartById")
public class DeleteShoppingCartById extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Init_Req_Resp_Config.init(request, response);

        // 1、从请求中捞取 order_id
        String shopping_cart_id = request.getParameter("shopping_cart_id");

        // 2、通过ShoppingCartDao 利用shopping_cart_id 对表中记录进行删除
        Shopping_cartDao shopping_cartDao = DaoFactory.getShopping_cartDaoInstance();
        Boolean flag = false;
        try {
            flag = shopping_cartDao.deleteById(Integer.parseInt(shopping_cart_id));

        } catch (Exception e) {
            e.printStackTrace();
        }


        // 3、响应结果
        Meta meta = new Meta();
        if (flag) {
            meta.setStatus(200);
            meta.setMsg("（"+shopping_cart_id+")删除订单记录成功");
        }
        else {
            meta.setStatus(500);
            meta.setMsg("（"+shopping_cart_id+")失败");
        }


        JSONObject jsonObject = new JSONObject();
        // 转换成json
        String backToBrowser = jsonObject.toJSONString(meta);

        // 响应数据
        response.getWriter().write(backToBrowser);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
